from math import exp, sin
from constant import *
from Agents import *


class ball(agent):
    def __init__(self,father):
        super().__init__(father)
        self.fv=1.56
        self.v=int(step*self.fv)
        self.base_size=(25,25)
        self.set_image('tennis')
        self.maxzoom=0.5
        self.minzoom=-0.3
        self.resize(self.base_size[0],self.base_size[1])
        self.move(int((right_x-left_x)/2),bottom)
        self.arrived=False

        self.h=1
        self.start_h=1
        self.oh=1
        self.maxh=3

        self.sx=0
        self.sy=0   
        self.tx=right_x
        self.ty=bottom
        self.dx=0
        self.dy=0

        self.movable=False

    
    def is_over(self):
        if self.x()<left_x-30 or self.x()>right_x+30 or self.y()<up-30 or self.y()>bottom+30:
            return True
        else:
            return False
    
    def breset(self,x,y):
        self.h=1
        self.start_h=1
        self.oh=1
        self.maxh=3

        self.sx=0
        self.sy=0   
        self.tx=right_x
        self.ty=bottom
        self.dx=0
        self.dy=0

        self.movable=False
        
        self.move(x,y)
        
        self.move(x,y)
    
    
    def dynamic_pic(self):
        pass
    

    def cal_h(self,dy):
        f=1
        if dy<=0:
            if self.y()>=mid:
                self.h=self.start_h+(self.maxh-self.start_h)*abs(float(self.y()-self.sy)/(self.sy-mid))
            else:
                self.h=(self.maxh)*abs(float(self.y()-self.ty)/(self.ty-mid))
        else:
            if self.y()<=mid:
                self.h=self.start_h+(self.maxh-self.start_h)*abs((self.y()-self.sy)/(self.sy-mid))
            else:
                self.h=(self.maxh)*abs((self.y()-self.ty)/(self.ty-mid))
        
        self.h=self.h if self.h<self.maxh else self.maxh
        f=(float(self.h-self.oh)/float(self.maxh-self.oh))*self.maxzoom+1
        f=f if f>=1+self.minzoom else 1+self.minzoom
        self.resize(int(f*self.base_size[0]),int(f*self.base_size[1]))


    def movd(self,dx,dy):
        x=self.x()+dx
        if x<left_x-30:
            x=left_x-30
        if x>right_x+30:
            x=right_x+30
        y=self.y()+dy

        if y>bottom+80:
            y=bottom+80
        if y<up-80:
            y=up-80
        

        self.move(x,y)
        self.px=self.x()
        self.py=self.y()
        self.cal_h(dy)

    
    def set_t(self,x,y):
        self.movable=True
        self.arrived=False
        self.sx=self.x()
        self.sy=self.y()
        self.start_h=self.h
        self.tx=x
        self.ty=y

    def execute_action(self):
        if self.movable:
            self.mov2pos(self.tx,self.ty)

    def mov(self,x,y):
        if x<left_x:
            x=left_x
        if x>right_x:
            x=right_x
        if y>bottom:
            y=bottom
        if y<up:
            y=up
        if self.type==1 and y<mid_down:
            y=mid_down

        if self.type==2 and y>mid_up:
            y=mid_up
        self.move(x,y)
        self.px=self.x()
        self.py=self.y()

    def mov2pos(self,x,y):
        try:
            if math.sqrt(math.pow((self.x()-x),2)+math.pow((self.y()-y),2))<self.v:
                self.arrived=True
            if self.arrived:
                self.movd(self.dx,self.dy)
                
            
                return
        except:
            print('mov error')
            exit(0)
        
        try:
            theta=abs(math.atan((float(y-self.y())/float(x-self.x()))))
            vx=int(self.v*math.cos(theta))
            vy=int(self.v*math.sin(theta))
        except:
            
            vx=0
            vy=int(self.v)
        
        vx=vx if x>self.x() else -vx
        vy=vy if y>self.y() else -vy

        try:
            self.movd(vx,vy)
            self.dx=vx
            self.dy=vy
        

            
        except:
            print( 'ball move error',x,y)

        
        

    
    
